﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen

Public Class ChuongTrinhKhuyenMai
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMTinhTrangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMTinhTrang)
        Me.DMKhachHangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKhachHang)
        Me.DMHangHoaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMHangHoa)
        Me.DoiTuongApDungTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DoiTuongApDung)
        Me.BHChuongTrinhKhuyenMaiDoiTuongApDungTableAdapter.Fill(Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMaiDoiTuongApDung)
        Me.BHChuongTrinhKhuyenMaiDMHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMaiDMHang)
        Me.BHChuongTrinhKhuyenMaiTableAdapter.Fill(Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMai)
        Me.IGridControlDs01.ForceInitialize()
        Me.IGridViewDs01.MoveLast()
        Me.setHeaderFooter("Công Ty CP Tư Vấn Giải Pháp ERP Phong Cách Việt", "Lâm Văn Bền, P.Tân Kiểng, Q.7, TP.HCM")
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub ChuongTrinhKhuyenMai_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
    End Sub

    Public Sub New()
        Me.IGridViewPopUp1.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(0)
        Me.setSoTab(2)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMai, Me.BHChuongTrinhKhuyenMaiBindingSource, Me.TableAdapterManager, Me.BHChuongTrinhKhuyenMaiTableAdapter, Me.MactkmITextEdit, Me.TinhtrangISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMaiDMHang, Me.FKBHChuongTrinma3D2915A8BindingSource, Me.BHChuongTrinhKhuyenMaiDMHangTableAdapter)
        Me.setTab2(Me.PCV_CLINICDataSet.BHChuongTrinhKhuyenMaiDoiTuongApDung, Me.FKBHChuongTrinma3E1D39E1BindingSource, Me.BHChuongTrinhKhuyenMaiDoiTuongApDungTableAdapter)
        Me.setPopUp1(Me.PCV_CLINICDataSet_Don.DMKhachHang, Me.DMKhachHangBindingSource, Me.DMKhachHangTableAdapter)
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "CTKM" & System.Guid.NewGuid().ToString)
        Me.NgaylapIDateEdit.EditValue = Today()
        Me.MactkmITextEdit.EditValue = Me.QueriesTableAdapter1.getMaCTKM_BHChuongTrinhKhuyenMai("CTKM", getNgayDauNam(Today), getNgayCuoiNam(Today))
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mactkm", Me.MactkmITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluongmua", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("giatrimua", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("lanmua", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("chietkhau", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluongtang", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("quydoithanhtien", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid())
    End Sub

    Private Sub IGridViewChiTiet02_InitNewRow(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet02.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mactkm", Me.MactkmITextEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("uid", System.Guid.NewGuid())
    End Sub

    Private Sub RepositoryItemISearchLookUpEdit_DMHang_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_DMHang.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowhandle, "ma"))
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowhandle, "ten"))
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowhandle, "donvitinh"))
    End Sub

    Private Sub BarButtonItemLuuPopUp1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuuPopUp1.ItemClick
        For index = 0 To Me.IGridViewPopUp1.RowCount - 1
            If CBool(Me.IGridViewPopUp1.GetRowCellValue(index, "Mark")) = True AndAlso Me.getKiemTraMaTonTaiIGridViewChitiet02(Me.IGridViewPopUp1.GetRowCellValue(index, "ma")) Then

                Me.IGridViewChiTiet02.SetFocusedRowCellValue("makhachhang", Me.IGridViewPopUp1.GetRowCellValue(index, "ma").ToString)
                Me.IGridViewChiTiet02.SetFocusedRowCellValue("tenkhachhang", Me.IGridViewPopUp1.GetRowCellValue(index, "ten").ToString)
                Me.IGridViewChiTiet02.SetFocusedRowCellValue("diachi", Me.IGridViewPopUp1.GetRowCellValue(index, "diachi").ToString)
                Me.IGridViewChiTiet02.AddNewRow()
            End If
            Me.IGridViewPopUp1.SetRowCellValue(index, "Mark", False)
        Next
        'delete row cuối cùng bị dư
        If IsDBNull(Me.IGridViewChiTiet02.GetFocusedRowCellValue("makhachhang")) Then
            Me.IGridViewChiTiet02.DeleteSelectedRows()
        End If
        'Duyệt xem trong grid có dòng dư
        For index = 0 To IGridViewChiTiet02.RowCount
            If IsDBNull(Me.IGridViewChiTiet02.GetRowCellValue(index, "makhachhang")) Then
                Me.IGridViewChiTiet02.DeleteRow(index)
            End If
        Next
    End Sub

    'Kiểm tra khách hàng đã tồn tại trong IGridViewChitiet02
    Private Function getKiemTraMaTonTaiIGridViewChitiet02(ByVal sMaKhachHang As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
        Dim acc As Integer
        For index = 0 To Me.IGridViewChiTiet02.RowCount - 2
            If Me.IGridViewChiTiet02.GetRowCellValue(index, "makhachhang").ToString.ToUpper = sMaKhachHang.ToUpper Then
                acc = acc + 1
            End If
        Next
        If acc > 0 Then
            Return False
        End If
        Return True
    End Function

    Private Sub BarButtonItemBaoCao_ItemClick_1(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Dim report As Report_ChuongTrinhKhuyenMai = New Report_ChuongTrinhKhuyenMai
        report.BhChuongTrinhKhuyenMaiTableAdapter1.FillMa_BHChuongTrinhKhuyenMai(report.PcV_CLINICDataSet1.BHChuongTrinhKhuyenMai, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.BhChuongTrinhKhuyenMaiDMHangTableAdapter1.FillMa_BHChuongTrinhKhuyenMaiDMHang(report.PcV_CLINICDataSet1.BHChuongTrinhKhuyenMaiDMHang, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.LoaiApDung.Value = Me.LoaiapdungISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.LoaiapdungISearchLookupEdit.EditValue)
        report.LoaiKhuyenMai.Value = Me.LoaikhuyenmaiISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.LoaikhuyenmaiISearchLookupEdit.EditValue)
        report.ShowRibbonPreview()
    End Sub
End Class
